package com.david.core.utils

import android.graphics.*

/**
 * 海报生成工具。
 * 根据设计稿设置尺寸大小、图片位置及大小、文本位置及大小颜色。云端图片借助三方工具glide／自己添加图片请求。
 * 使用示例：
 * val bg = BitmapFactory.decodeResource(resources, R.drawable.bg)
    val avatar = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_round)
    val avatarLabel = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_round)
    val nameLabel = BitmapFactory.decodeResource(resources, R.drawable.my_ic_deposit)
    val qrImg = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
    val text = BitmapFactory.decodeResource(resources, R.drawable.my_ic_text)
    val bitmap = PosterCreator.Builder()
    .setDesignWidth(1125)
    .setDesignHeight(2436)
    .addRect(Color.WHITE, Paint.Style.FILL, 0f, RectF(0F, 2028F, 1125F, 2436F))
    .addRect(Color.parseColor("#FF5800"), Paint.Style.FILL, 0f, RectF(96f, 2235f, 96f, 2319f))
    .addImage(bg, Rect(0, 0, bg.width, bg.height), Rect(0, 0, 1125, 2028))
    .addImage(avatar, Rect(0, 0, avatar.width, avatar.height), Rect(72, 300, 288, 516))
    .addImage(avatarLabel, Rect(0, 0, avatarLabel.width, avatarLabel.height), Rect(240, 468, 288, 516))
    .addImage(nameLabel, Rect(0, 0, nameLabel.width, nameLabel.height), Rect(240, 468, 288, 516))
    .addImage(qrImg, Rect(0, 0, qrImg.width, qrImg.height), Rect(813, 2118, 1035, 2331))
    .addImage(text, Rect(0, 0, text.width, text.height), Rect(96, 2124, 390, 2196))
    .addTextBean(Color.parseColor("#24292B"),
    DpUtils.dp2Px(this, 24f).toFloat(), Paint.Style.FILL, "haha", 336f, false, 390f)
    .addTextBean(Color.parseColor("#24292B"), DpUtils.dp2Px(this, 18f).toFloat(), Paint.Style.FILL, "2022/3/3", 336f, false, 480f)
    .create()
 *
 *
 * 获取在线图片bitmap示例：
 * val inputStream = URL("").openStream()
    val bitmap = BitmapFactory.decodeStream(inputStream)
    inputStream.close()
 * 注：要切换到子线程获取，有多张在线图片时开启线程池获取，添加已下载的数量标记判断是否都下载完成。
 * volatile int num = 0;
 * kotlin：
 * @Volatile
    var num = 0
 * 每个task判断num是否为要下载的图片总数来决定是否开始生成bitmap。
 */
class PosterCreator {

    class Builder {
        private var width = 0//生成宽高
        private var height = 0
        private var images: ArrayList<ImageBean> = ArrayList()
        private var texts: ArrayList<TextBean> = ArrayList()
        private var rects: ArrayList<RectBean> = ArrayList()
        private var lastPaintRects: ArrayList<RectBean> = ArrayList()//悬浮在最上方的rect图形
        private var mPaint: Paint = Paint()

        init {
            mPaint.isAntiAlias = true //设置抗锯齿
        }

        fun setDesignWidth(designWidth: Int): Builder{
            width = designWidth
            return this
        }

        fun setDesignHeight(designHeight: Int): Builder{
            height = designHeight
            return this
        }

        fun addImage(img: Bitmap, /*选择图片被绘制的区域*/imgRect: Rect, drawRect: Rect//在画布上的区域
        ): Builder {
            images.add(ImageBean(img, imgRect, drawRect))
            return this
        }

        fun addTextBean(color: Int, size: Float, style: Paint.Style, text: String, startX: Float, isCenterX: Boolean = false, startY: Float, isBold:Boolean = false): Builder {
            texts.add(TextBean(color, size, style, text,isCenterX, startX, startY, isBold))
            return this
        }

        fun addRect(color: Int, style: Paint.Style, radius: Float, rect: RectF): Builder {
            rects.add(RectBean(color, style, radius, rect))
            return this
        }

        fun addLastPaintRect(color: Int, style: Paint.Style, radius: Float, rect: RectF): Builder {
            lastPaintRects.add(RectBean(color, style, radius, rect))
            return this
        }

        fun getPaint(): Paint{
            return mPaint
        }

        fun create(): Bitmap {
            val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
            val canvas = Canvas(bmp)
            rects.map {
                mPaint.color = it.color
                mPaint.style = it.style
                canvas.drawRoundRect(it.rect, it.radius, it.radius, mPaint)
            }

            images.map {
                canvas.drawBitmap(it.img, it.imgRect, it.drawRect, mPaint)
            }

            texts.map {
                mPaint.style = it.style
                mPaint.textSize = it.size
                mPaint.color = it.color

                if (it.isBold) {
                    mPaint.typeface = Typeface.DEFAULT_BOLD
                } else {
                    mPaint.typeface = Typeface.DEFAULT
                }

                canvas.drawText(it.text, it.startX, it.startY, mPaint)
            }

            lastPaintRects.map {
                mPaint.color = it.color
                mPaint.style = it.style
                canvas.drawRoundRect(it.rect, it.radius, it.radius, mPaint)
            }
            return bmp
        }
    }

    data class ImageBean(var img: Bitmap, var imgRect: Rect,/*选择图片被绘制的区域*/var drawRect: Rect/*在画布上的区域*/)

    data class TextBean(var color: Int, var size: Float, var style: Paint.Style, var text: String,val isCenterX: Boolean, var startX: Float, var startY: Float, var isBold:Boolean = false)

    /**
     * 纯色背景：矩形、圆角矩形、圆形、圆环、圆角矩形环
     */
    data class RectBean(var color: Int, var style: Paint.Style, var radius: Float, var rect: RectF)
}